//
//  WOTimeViewController.m
//
//  Created by Olof Millberg on 2010-03-25.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WOTimeViewController.h"

// View Controllers
#import "WONewTimeReportViewController.h"

// Sections
#import "WOTimeSection.h"

// Cells
#import "TimeReportCell.h"

// Other
#import "WOTimeReport.h"

@implementation WOTimeViewController

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [super tableView:aTableView cellForRowAtIndexPath:indexPath];
	
	WOTimeReport *timeReport = [workOrder.timeReports objectAtIndex:indexPath.row];
	
	NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	[dateFormatter setDateFormat:@"EEE d MMM YYYY"];
	((TimeReportCell *)cell).dateLabel.text = [dateFormatter stringFromDate:timeReport.entryDate];
	
	NSTimeInterval totalTime = 0;
	for (WOTimeInterval *ti in timeReport.timeIntervals)
		totalTime += ti.seconds;
	NSInteger hours = totalTime / 3600;
	NSInteger minutes = (totalTime - hours * 3600) / 60;
	((TimeReportCell *)cell).timeLabel.text = [NSString stringWithFormat:@"%i tim %i min", hours, minutes];
	
	((TimeReportCell *)cell).commentLabel.text = timeReport.comment;

	return cell;
}

- (void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	WONewTimeReportViewController *timeReportingVC = [[WONewTimeReportViewController alloc] initWithWorkOrder:self.workOrder];
	timeReportingVC.timeReport = [workOrder.timeReports objectAtIndex:indexPath.row];
	
	[self.tabBarController.navigationController pushViewController:timeReportingVC animated:YES];
	
	[timeReportingVC release];
}

- (void) reportTime:(id)sender
{
	WONewTimeReportViewController *timeReportingVC = [[WONewTimeReportViewController alloc] initWithWorkOrder:self.workOrder];
	
	[self.tabBarController.navigationController pushViewController:timeReportingVC animated:YES];
	
	[timeReportingVC release];
}

- (id) init
{
	if (self = [super initWithStyle:UITableViewStylePlain])
	{
		self.title = @"Tid";
		
		UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Tid" image:[UIImage imageNamed:@"tabbar_time.png"] tag:3];
		
		self.tabBarItem = theItem;
		[theItem release];
	}
	
	return self;
}

- (void) loadView
{
	[super loadView];
	
	self.tableView.separatorColor = [UIColor groupTableViewBackgroundColor];
	self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
	
	self.tableView.rowHeight = 64;
	
	self.sections = [NSMutableArray array];
	[self.sections addObject:[WOTimeSection timeSectionWithList:workOrder.timeReports]];
}

- (void) viewWillAppear:(BOOL)animated
{
	[self.tabBarController.navigationItem setRightBarButtonItem:
	 [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
													target:self 
													action:@selector(reportTime:)] autorelease] animated:YES];
	[self.tableView reloadData];
}

- (void) viewDidAppear:(BOOL)animated
{
	self.tabBarController.title = self.title;
	[[NSUserDefaults standardUserDefaults] setInteger:2 forKey:@"currentTab"];
}

- (void) viewWillDisappear:(BOOL)animated
{
	[self.tabBarController.navigationItem setRightBarButtonItem:nil animated:YES];
}

@end
